package org.reborn.model

import TileLOD._
import org.reborn.Constants._

import java.util.Date


class Tile(val xIndex:Int,val yIndex:Int,val lod:TileLOD) {
  //Pre-compute these values because they will be used a lot
//  val pos = new Point2D(xIndex * TILE_LENGTH + HALF_TILE_LENGTH, yIndex * TILE_LENGTH + HALF_TILE_LENGTH)  
//  val pos_ul = new Point2D(xIndex * TILE_LENGTH , (yIndex+1) * TILE_LENGTH )
//  val pos_ur = new Point2D((xIndex+1) * TILE_LENGTH , (yIndex+1) * TILE_LENGTH )
//  val pos_bl = new Point2D(xIndex * TILE_LENGTH , yIndex * TILE_LENGTH )
//  val pos_br = new Point2D((xIndex+1) * TILE_LENGTH , yIndex * TILE_LENGTH )
 
  var lastUpdate:Option[Date] = None
    
  var left:Option[Tile] = None;
  var right:Option[Tile] = None;
  var top:Option[Tile] = None;
  var bottom:Option[Tile] = None;

  var parent:Option[Tile] = None;
  
  var leftChild:Option[Tile] = None;
  var rightChild:Option[Tile] = None;
  var topChild:Option[Tile] = None;
  var bottomChild:Option[Tile] = None;

  private var column:Option[TerrainColumn] = None;
  
  
  def hasChildren:Boolean = { ! leftChild.isEmpty }
  def hasParent:Boolean = { ! parent.isEmpty }
  def topParent:Tile = {
    def getTopParent(current:Tile):Tile = {
      if(current hasParent) getTopParent(current) else current
    }
    getTopParent(this)
  } 
  
  def setColumn(column:TerrainColumn){
    if(column==null) this.column = None
    else this.column = Some(column)
  }
  
  def getColumn():TerrainColumn = column.get
}
